// readFile
package readFile

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

func GetStockIDFromFile(filePath string) ([]string, error) {
	var result []string
	file, err := os.Open(filePath)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	info := bufio.NewReader(file)
	//var line string
	//var err1 error
	for {
		line, err1 := info.ReadString('\n')
		if err1 != nil {
			if err1 == io.EOF {
				break
			}
			return nil, err
		}
		line = strings.TrimSpace(line)
		line = strings.Replace(line, "\n", "", -1)
		result = append(result, line)
	}
	return result, nil
}
func GetAllFile(pathname string) ([]string, error) {
	rd, err := ioutil.ReadDir(pathname)
	var stockid []string
	for _, fi := range rd {
		if fi.IsDir() {
			fmt.Printf("[%s]\n", pathname+"\\"+fi.Name())
			GetAllFile(pathname + fi.Name() + "\\")
		} else {
			if len(fi.Name()) == 14 {
				stockid = append(stockid, strings.Split(fi.Name(), ".")[0])
			}
		}
	}
	return stockid, err
}
func toFile(f *os.File, sto string) error {

	//var err3 error
	filename := "c:/stock.txt"
	f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModeAppend)
	if err != nil {
		//log
		return nil
	}
	_, err = f.WriteString(sto + "\n")
	if err != nil {
		//log
		return nil
	}
	//	if checkFileIsExist(filename) { //如果文件存
	//		err3 = os.Remove(filename)    //打开文件
	//		f, err3 = os.Create(filename) //打开文件
	//	} else {
	//		f, err3 = os.Create(filename) //创建文件
	//	}
	//f.Write()
	//		_, err3 = f.WriteString(sto + "\r\n")
	//		if err3 != nil {
	//			return err3
	//
	//	}

	return nil

}
func checkFileIsExist(filename string) bool {
	var exist = true
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		exist = false
	}
	return exist
}
